#-*- coding=utf-8 -*-
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
from matplotlib import pyplot as plt
import time
import queue
import os
import threading


# 第一条线程
# 检测data.txt文件,把发生变化的数据保存到一个全局队列__global_thread_datas__里
def monitor_data_txt():
    data_file_list = os.listdir("D:/hello Python/邓老师任务/test5")
    if data_file_list.count("data.txt") == 0 :
        data_dict = data_txt_save()
        return data_dict

# 对发生变化的data.txt文件的数据进行记录
def data_txt_save():
    data_file = open("D:/hello Python/邓老师任务/test5/data.txt","r")
    file_num =[]
    file_data = []
    file_remake = []
    for each_line in data_file :
       if each_line.find("NULL") != -1 :
            each_line_list = each_line.split("\t")
            file_num.append(int(each_line_list[0]))
            file_data.append(float(each_line_list[1]))
            file_remake.append(each_line_list[2][:4])
    datas_list = []
    data_file.close()
    data_num = 0
    while data_num <= 9 :
        datas_list.append([file_num[data_num],file_data[data_num],file_remake[data_num]])
        data_num += 1
    data_dict = {"timestamp":data_file.readline(1),"datas":datas_list}
    print(data_dict)
    return data_dict

# 向__global_thread_datas__里添加数据
def main_add_data(__global_thread_datas__):
    while True:
        data_dict = monitor_data_txt()
        if data_dict != None :
            __global_thread_datas__.put(data_dict)
            print("队列数据个数：%s"%__global_thread_datas__ .qsize())
            time.sleep(0.5)



# 第二条线程
# def 
#



if __name__ == '__main__':
    # 全局变量
    __global_thread_datas__ = queue.Queue(maxsize=0)
    main_add_data(__global_thread_datas__)
